package com.million.eduservice.controller;


import com.million.commonutils.R;
import com.million.eduservice.entity.EduChapter;
import com.million.eduservice.entity.vo.chapter.ChapterVo;
import com.million.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-03-24
 */
@Api(tags = "课程大纲管理")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    //课堂大纲列表，根据课程id查询 ，可以用来回显数据
    @ApiOperation(value = "根据课程Id查询章节和小节")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideoByCourseId (@PathVariable("courseId") String courseId) {
        List<ChapterVo> getChapterVideo = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("getChapterVideo", getChapterVideo);
    }

    //添加章节信息
    @ApiOperation(value = "添加章节信息")
    @PostMapping("addChapter")
    public R addChapter (@RequestBody EduChapter eduChapter) {
        boolean save = eduChapterService.save(eduChapter);
        if (save) {
            return R.ok();
        } else {
            return R.error().message("添加章节信息失败");
        }
    }

    //根据章节id查询
    @GetMapping("getChapterById/{chapterId}")
    @ApiOperation(value = "根据章节id查询")
    public R getChapterById (@PathVariable("chapterId") String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("eduChapter", eduChapter);
    }

    //修改章节信息  解决修改是，自动填充的修改时间失效，需要updateById()中的对象有值才行
    @ApiOperation(value = "修改章节信息")
    @PostMapping("updateChapter")
    public R updateChapter (@RequestBody ChapterVo chapterVo) {
        eduChapterService.updateChapter(chapterVo);
        return R.ok();
    }

    //根据章节下面是否有小节判断是否删除，有=》不删
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter (@PathVariable("chapterId") String chapterId) {
       boolean flag =  eduChapterService.deleteChapter(chapterId);
       if(flag){
           return R.ok();
       }else{
           return R.ok().message("删除失败");
       }
    }
}

